package com.assessment.servermanager.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.assessment.servermanager.commen.entity.PageQuery;
import com.assessment.servermanager.commen.exception.ServiceException;
import com.assessment.servermanager.commen.page.TableDataInfo;
import com.assessment.servermanager.commen.utils.StringUtils;
import com.assessment.servermanager.entity.TbDoc;
import com.assessment.servermanager.entity.bo.TbDocBo;
import com.assessment.servermanager.entity.vo.TbDocVo;
import com.assessment.servermanager.mapper.TbDocMapper;
import com.assessment.servermanager.oss.core.OssClient;
import com.assessment.servermanager.oss.entity.UploadResult;
import com.assessment.servermanager.oss.entity.vo.SysOssVo;
import com.assessment.servermanager.oss.factory.OssFactory;
import com.assessment.servermanager.service.ITbDocService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author jiajia
 * @date 2023-08-28
 */
@Service
public class TbDocServiceImpl implements ITbDocService {
    @Autowired
    private TbDocMapper tbDocMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TbDoc selectTbDocById(Long id) {
        return tbDocMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tbDocBo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public TableDataInfo<TbDocVo> selectTbDocList(TbDocBo tbDocBo, PageQuery pageQuery) {
        QueryWrapper<TbDoc> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(ObjectUtil.isNotNull(tbDocBo.getCreateDate()),"createTime between"+ tbDocBo.getStartTime() +"and"+ tbDocBo.getEndTime());
        queryWrapper.eq(StringUtils.isNotEmpty(tbDocBo.getFileName()),"doc.file_name", tbDocBo.getFileName());
        queryWrapper.eq(StringUtils.isNotEmpty(tbDocBo.getRemark()),"doc.remark", tbDocBo.getRemark());
        queryWrapper.eq(StringUtils.isNotEmpty(tbDocBo.getTitle()),"doc.title", tbDocBo.getTitle());
        queryWrapper.eq(tbDocBo.getUid() != null,"doc.uid", tbDocBo.getUid());
        queryWrapper.eq(StringUtils.isNotEmpty(tbDocBo.getVideo_url()),"doc.video_url",tbDocBo.getVideo_url());
        queryWrapper.eq(StringUtils.isNotEmpty(tbDocBo.getFile_url()),"doc.file_url",tbDocBo.getFile_url());
        queryWrapper.eq("ad.delFlag",0);
        Page<TbDocVo> page = tbDocMapper.selectPageList(pageQuery.build(),queryWrapper);
        return TableDataInfo.build(page);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tbDoc 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTbDoc(TbDoc tbDoc) {

        return tbDocMapper.insert(tbDoc);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tbDoc 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTbDoc(TbDoc tbDoc) {
        return tbDocMapper.updateById(tbDoc);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbDocByIds(Long[] ids) {
        return tbDocMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbDocById(Long id) {
        return tbDocMapper.deleteById(id);
    }



    @Override
    public SysOssVo uploadFile(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        OssClient storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
    }

    private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) {
      /*  SysOss oss = new SysOss();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName(originalfileName);
        oss.setService(configKey);
        baseMapper.insert(oss);
        SysOssVo sysOssVo = BeanUtil.toBean(oss, SysOssVo.class);
        return this.matchingUrl(sysOssVo);*/
        SysOssVo ossVo = new SysOssVo();
        ossVo.setUrl(uploadResult.getUrl());
        ossVo.setFileSuffix(suffix);
        ossVo.setFileName(uploadResult.getFilename());
        ossVo.setOriginalName(originalfileName);
        ossVo.setService(configKey);
       /* TbDoc tbDoc = new TbDoc();
        tbDoc.setCreateDate(new Date());
        tbDoc.setFileName(uploadResult.getFilename());*/
        return ossVo;
    }
}
